package com.imooc.service;

import com.imooc.domain.Girl;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.GirlException;
import com.imooc.repository.GirlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * Created by echo on 2017/5/8.
 */
@Service
public class GirlService
{
    @Autowired
    private GirlRepository girlRepository;

    @Transactional
    public void insertTwo()
    {
        Girl girlA = new Girl();
        girlA.setCupSize("A");
        girlA.setAge(19);
        girlRepository.save(girlA);

        Girl girlB = new Girl();
        girlB.setCupSize("B");
        girlB.setAge(20);
        girlRepository.save(girlB);
    }

    public void getAge(Integer id) throws Exception
    {
        Optional<Girl> optional = girlRepository.findById(id);
        if (optional.isPresent())
        {
            Girl girl = optional.get();
            Integer age = girl.getAge();
            if (age < 10)
            {
                throw new GirlException(ResultEnum.PRIMARY_SCHOOL);
            }
            else if (age > 10 && age < 16)
            {
                throw new GirlException(ResultEnum.MIDDLE_SCHOOL);
            }
        }
        else
        {

        }
    }

    /**
     * 通过Id查询一个女生的信息
     * @param id
     * @return
     */
    public Girl findOne(Integer id)
    {
        return girlRepository.findById(id).get();
    }
}
